package com.woniuxy.classdeans.controller;

import com.woniuxy.classdeans.exception.ClassdeansException;
import com.woniuxy.classdeans.service.TeamInstructorService;
import com.woniuxy.commons.entity.ResponseResult;
import com.woniuxy.commons.entity.wyh.ResponseTable;
import com.woniuxy.commons.entity.wyh.TeamInstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级任课老师表
 * @author 王艳辉
 * @date 2020/7/31 - 10:24
 */
@RestController
@RequestMapping("TeamInstructor")
@Transactional
@Slf4j
public class TeamInstructorController {
    @Autowired
    private TeamInstructorService teamInstructorService;



    /**
     * 新增班级任课老师信息
     * @param teamInstructor
     * @return
     */
    @RequestMapping("addTeamInstructor")
    public Object addTeamInstructor(TeamInstructor teamInstructor){
        return teamInstructorService.addTeamInstructor(teamInstructor);
    }

    /**
     * 更新班级任课老师表
     * @param teamInstructor
     * @return
     */
    @RequestMapping("updateTeamInstructor")
    public Object updateTeamInstructor(TeamInstructor teamInstructor){
        //返回消息类型,默认失败
        ResponseResult<TeamInstructor> res = new ResponseResult<TeamInstructor>().setCode(400).setMessage("更新失败！");
        try {
            int num = teamInstructorService.updateTeamInstructor(teamInstructor);
            if(num==1){

                //获取更新后的班级任课老师信息
                teamInstructor = teamInstructorService.getByTeid(teamInstructor.getTeId());
                //数据返回给前端
                res.setCode(200).setMessage("班级任课老师信息成功！").setData(teamInstructor);
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("TeamInstructor/updateTeamInstructor异常");
        }


        return res;
    }


    /**
     * 根据teid获取班级排课信息
     * @param TeId 班级id
     * @return
     */
    @RequestMapping("getByTeid")
    public  Object getByTeid(int TeId){
        //返回消息类型
        ResponseResult<List<TeamInstructor>> res = new ResponseResult<List<TeamInstructor>>().setCode(400).setMessage("查询失败！");

        try {
            TeamInstructor teamInstructor = teamInstructorService.getByTeid(TeId);
            //封装成数组
            ArrayList<TeamInstructor> teamInstructors = new ArrayList<>();
            //获取数据成功
            if(teamInstructor!=null){
                teamInstructors.add(teamInstructor);
                res.setData(teamInstructors).setCode(0).setMessage("获取成功");//设置数据返回
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("TeamInstructor/getByTeid异常");
        }


        return res;
    }

    /**
     * 查询所有班级排课信息
     * @return
     */
    @RequestMapping("getAllMsg")
    public  Object getAllMsg(){
        //返回消息类型
        ResponseTable<TeamInstructor> res = new ResponseTable<TeamInstructor>().setCode(400).setMsg("查询失败！");

        try {
            //获取所有未处理信息
            List<TeamInstructor> teamInstructor = teamInstructorService.getAll();

            //获取数据成功
            if(teamInstructor!=null){

                res.setData(teamInstructor).setCode(0).setMsg("获取成功").setCount(teamInstructor.size());//设置数据返回
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("TeamInstructor/getAllMsg异常");
        }

        return res;
    }

}
